//
//  Participate.m
//  WhoPaidfor
//
//  Created by user on 9/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Participate.h"
#import "SBJsonParser.h"
@implementation Participate
#define SERVER_URL @"http://nghexaydung.com/whopaid/public/"
static Participate *_instance = nil;  // <-- important 
@synthesize name;
@synthesize num;
@synthesize money;
@synthesize id_;

-(id)initWithID:(int)id_
{
    if (self = [super init])
    {
        self.name = @"Username";
        self.num = 0;
        self.money = @"0";
        self.id_ = 0;
    }
    return self;
}

-(NSString*)description
{
    return [NSString stringWithFormat:@"name: %@, num: %d", self.name, self.num];
}

-(void)dealloc
{
    self.name = nil;
    self.money = nil;
    //self.id_ = nil;
    [super dealloc];
}

+(Participate *)instance
{ 
	// skip everything
	if(_instance) return _instance; 
    
	// Singleton
	@synchronized([Participate class]) 
	{
		if(!_instance)
		{
			_instance = [[self alloc] init];
            
            //	NSLog(@"Creating global instance!"); <-- You should see this once only in your program
		}
        
		return _instance;
	}
    
	return nil;
}
-(NSMutableArray *)getDatasWS:(int)entry_id
{
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/expenses/getparticipants?expenses_id=%d", SERVER_URL, entry_id]; 
    NSLog(@" get list participant %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSArray *results = [parser objectWithString:json_string error:nil];
    for (NSDictionary *obj in results)
    {
        NSString *value = [NSString stringWithFormat:@"%d#%@#%d", [[obj objectForKey:@"user_id"] intValue], [obj objectForKey:@"user_name"], [[obj objectForKey:@"quantity"] intValue] ];
        [listData addObject:value];
    }
    return listData;
}
-(void) addForEvent:(int)event_id ArrayMember:(NSMutableArray *)pars
{
    User *user = [User instance];
    for(int i=0;i<[pars count];i++){
        ParticipateClass *p = [pars objectAtIndex:i];
        p.name = [p.name stringByReplacingOccurrencesOfString:@" "
                                               withString:@"%20"];
        NSMutableString *postString;
        if(![p.idString isEqualToString:@""]){
            p.firstMail = [NSString stringWithFormat:@"%@@facebook.com",p.idString];
            postString = [NSMutableString stringWithFormat:@"event_id=%d&user_id=%d&name=%@&email=%@&image=%@&facebookid=%@",event_id, user._id, p.name, p.firstMail,@"",p.idString];
        }else{
            postString = [NSMutableString stringWithFormat:@"event_id=%d&user_id=%d&name=%@&email=%@&image=%@&facebookid=%@",event_id, user._id, p.name, p.firstMail,@"contact",@""];
        }
        
        NSString *urlString = [NSString stringWithFormat:@"%@user/addmember?",SERVER_URL];
        urlString = [urlString stringByAppendingFormat:@"%@", postString];
        NSLog(@"save par %@",urlString);
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    }
}
@end
